﻿<div ng-controller="InfoCollection_AddC">
    <div class="bg-light lter b-b wrapper-md">
        <span class="m-n font-thin h3">添加钢厂采集信息</span>&nbsp;&nbsp;&nbsp;&nbsp;
        <!--<span class="text-right h3">{{time | date:"yyyy-MM-dd"}}</span>-->
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <!--<div class="panel-heading">-->
                            <!--<span class="h4">添加信息</span>-->
                        <!--</div>-->
                        <div class="input-group">
                            <span class="input-group-addon">编辑日期</span>
                            <input type="date" class="form-control" ng-model="time" ng-change="change()">
                        </div>
                        <div class="input-group">
                            <span class="input-group-addon">钢厂名称</span>
                            <select id="Provider" required="required" class="form-control" ng-options="x for x in providerdata" ng-model="Supplier" ng-change="change()">
                            </select>
                        </div>
                        <div class="panel-body">
                            <div class="row text-center" style="margin-top: 14px; background-color: #CCCCCC">
                                <span style="height: 26px;border: 1px solid #999" class="col-xs-4">种类</span>
                                <span style="height: 26px;border: 1px solid #999" class="col-xs-4">库存(吨)</span>
                                <span style="height: 26px;border: 1px solid #999" class="col-xs-4">出货量(车)</span>
                            </div>
                            <div class="row text-center" ng-repeat="item in newarr">
                                <span style="height: 26px;border: 1px solid #999;" class="col-xs-4">{{item.Name}}</span>
                                <input type="number" class="col-xs-4 text-center" min="0" ng-model="item.Stock">
                                <input type="number" class="col-xs-4 text-center" min="0" ng-model="item.Volume">
                            </div>
                            <h3>贸易商</h3>

                            <div class="row text-center" style="margin-top: 14px; background-color: #CCCCCC">
                                <span style="height: 26px;border: 1px solid #999" class="col-xs-4">名称</span>
                                <span style="height: 26px;border: 1px solid #999" class="col-xs-4">材料名</span>
                                <span style="height: 26px;border: 1px solid #999" class="col-xs-4">出货量(车)</span>
                            </div>
                            <div class="row text-center" ng-repeat="item in dataArr track by $index">
                                <input type="text" class="col-xs-4 text-center" ng-model="item.AgentName">
                                <select style="height: 26px;" class="col-xs-4 text-center" ng-options="x for x in selectdata" ng-model="item.Name"></select>
                                <input type="number" class="col-xs-4 text-center" min="0" ng-model="item.Volume">
                            </div>
                            <div class="row text-center" style="margin-top: 10px;">
                                <button class="btn btn-default" ng-click="addrow()">新增行</button>
                            </div>
                        </div>
                        <footer class="panel-footer text-right bg-light lter" style="text-align:center">
                            <button type="button" class="btn btn-success" ng-click="add()" ng-disabled="Supplier == '' || Supplier == undefined">提交</button>
                            <span style="padding:0 5%"></span>
                            <button type="button" class="btn btn-default" ng-click="abolish()">取消</button>
                        </footer>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    angular.module('app')//非标优惠
.controller("InfoCollection_AddC", function ($scope, $state, $http) {
    $scope.time = new Date();
    $scope.Supplier = "";
    $scope.selectdata = ["","螺纹钢","线材","钢坯","盘螺"];
    $scope.newarr = [
        {type: "建材", SupplierName: null, AgentName: null, Name: "螺纹钢",	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "建材", SupplierName: null, AgentName: null, Name: "线材",	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "建材", SupplierName: null, AgentName: null, Name: "钢坯",	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "建材", SupplierName: null, AgentName: null, Name: "盘螺",	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "建材", SupplierName: null, AgentName: null, Name: "建材总计",	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "原料", SupplierName: null, AgentName: null, Name: "矿粉",	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "原料", SupplierName: null, AgentName: null, Name: "焦炭",	Price: null, Stock: null, Volume: null, Remark: null}
    ];
    $scope.dataArr = [
        {type: "代理商", SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "代理商", SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null},
        {type: "代理商", SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null}
    ];
    $scope.addrow = function(){
        $scope.dataArr.push({type: "代理商", SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null})
    };
    $scope.newdata = [];
//    获取供应商
    $http({
        url: "../api/InfoCollection/GetFieldManProviders",
        method:"GET"
    })
            .then(function (res) {
                if(res.State<=0){
                    alert(res.Msg);
                }else {
                    $scope.providerdata = res.data;
                    $scope.Supplier = $scope.providerdata[0];
//                    console.log($scope.providerdata[0]);
                    $scope.change();
                }
            })
            .then(function (error) {
                if(error){
                    console.log("error");
                }
            });

    $scope.change = function(){
        $scope.newarr = [
            {type: "建材", SupplierName: null, AgentName: null, Name: "螺纹钢",	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "建材", SupplierName: null, AgentName: null, Name: "线材",	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "建材", SupplierName: null, AgentName: null, Name: "钢坯",	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "建材", SupplierName: null, AgentName: null, Name: "盘螺",	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "建材", SupplierName: null, AgentName: null, Name: "建材总计",	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "原料", SupplierName: null, AgentName: null, Name: "矿粉",	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "原料", SupplierName: null, AgentName: null, Name: "焦炭",	Price: null, Stock: null, Volume: null, Remark: null}
        ];
        $scope.dataArr = [
            {type: "代理商", SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "代理商", SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null},
            {type: "代理商", SupplierName: null, AgentName: null, Name: null,	Price: null, Stock: null, Volume: null, Remark: null}
        ];
        if ($scope.Supplier) {


            $http.get("/api/InfoCollection/GetInfo", { params: { SupplierName: $scope.Supplier, dt: $scope.time } })
                .then(function(res) {
                    if (res.State <= 0) {
                        alert(res.Msg);
                    } else {
                        console.log(res.data.Data);
                        $scope.newdata1 = res.data.Data;
                        if ($scope.newdata1.length > 0) {
                            for (var i = 0; i < $scope.newarr.length; i++) {
                                for (var j = 0; j < $scope.newdata1.length; j++) {
                                    if ($scope.newarr[i].type == $scope.newdata1[j].Type && $scope.newarr[i].Name == $scope.newdata1[j].Name) {
                                        $scope.newarr[i] = $scope.newdata1[j];
                                    }
                                }
                            }
                            $scope.dataArr = [];
                            for (var j = 0; j < $scope.newdata1.length; j++) {
                                if ("代理商" == $scope.newdata1[j].Type) {
                                    $scope.dataArr.push($scope.newdata1[j])
                                }
                            }
                            if ($scope.dataArr.length < 1) {
                                $scope.dataArr = [
                                    { type: "代理商", SupplierName: null, AgentName: null, Name: null, Price: null, Stock: null, Volume: null, Remark: null },
                                    { type: "代理商", SupplierName: null, AgentName: null, Name: null, Price: null, Stock: null, Volume: null, Remark: null },
                                    { type: "代理商", SupplierName: null, AgentName: null, Name: null, Price: null, Stock: null, Volume: null, Remark: null }
                                ];
                            }
                        } else {
                            $scope.newarr = [
                                { type: "建材", SupplierName: null, AgentName: null, Name: "螺纹钢", Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "建材", SupplierName: null, AgentName: null, Name: "线材", Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "建材", SupplierName: null, AgentName: null, Name: "钢坯", Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "建材", SupplierName: null, AgentName: null, Name: "盘螺", Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "建材", SupplierName: null, AgentName: null, Name: "建材总计", Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "原料", SupplierName: null, AgentName: null, Name: "矿粉", Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "原料", SupplierName: null, AgentName: null, Name: "焦炭", Price: null, Stock: null, Volume: null, Remark: null }
                            ];
                            $scope.dataArr = [
                                { type: "代理商", SupplierName: null, AgentName: null, Name: null, Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "代理商", SupplierName: null, AgentName: null, Name: null, Price: null, Stock: null, Volume: null, Remark: null },
                                { type: "代理商", SupplierName: null, AgentName: null, Name: null, Price: null, Stock: null, Volume: null, Remark: null }
                            ];
                        }


                    }
                })
                .then(function(error) {
                    if (error) {
                        console.log("error");
                    }
                });
        }
    };

    $scope.add = function () {
        console.log($scope.Supplier);
        $scope.ispost = true;
//        $scope.newdata = $scope.newdata.concat($scope.newarr,$scope.dataArr);
        for (var i=0;i<$scope.newarr.length;i++)
        {
            if ($scope.newarr[i].Stock != null || $scope.newarr[i].Volume != null){
                $scope.newarr[i].SupplierName = $scope.Supplier;
                $scope.newdata.push($scope.newarr[i])
            }
        }
        for (var i=0;i<$scope.dataArr.length;i++)
        {
            if ($scope.dataArr[i].AgentName != null && $scope.dataArr[i].AgentName != ""){
                if($scope.dataArr[i].Volume != null){
                    $scope.dataArr[i].SupplierName = $scope.Supplier;
                    $scope.newdata.push($scope.dataArr[i])
                }else{
                    alert("贸易商出货量不能为空");
                    $scope.ispost = false;
                    break;
                }
            }
        }
        console.log($scope.newdata);

        if($scope.ispost){
            $http.post("../api/InfoCollection/SetInfoByReplace?dt="+$scope.time.format("yyyy-MM-dd hh:mm:ss"), $scope.newdata)
                    .then(function (res) {
                        if (res.State <= 0) {
                            alert(res.Msg);
                        } else {
                            alert("保存成功");
                            $state.go("app.InfoCollection_List");
                        }
                        $scope.newdata = [];
                    },function (error) {
                        alert("处理失败");
                        $scope.newdata = [];
                    });
        }
    };



//    $scope.data = {};
//    $scope.data.Type = 0;
//        $scope.data.InfoDate = new Date();
//    $scope.add = function () {
//        console.log($scope.data);
//        $http.post("/api/InfoCollection/Add", $scope.data)
//       .then(function (res) {
//           if (res.State <= 0) {
//               alert(res.Msg);
//           } else {
//               alert("保存成功");
//               $state.go("app.InfoCollection_List");
//           }
//
//       },function (error) {
//           alert("处理失败");
//       });
//    };
    $scope.abolish = function(){
        $state.go("app.InfoCollection_List");
    };
    })

</script>